﻿using System;

class BinaryDigitsCount
{
    static void Main(string[] args)
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

        char bitToSearchfor = Convert.ToChar(Console.ReadLine());
        int numbersToSearchIn = int.Parse(Console.ReadLine());
        int[] resultsArray = new int[numbersToSearchIn];

        for (int i = 0; i < numbersToSearchIn; i++)
        {
            uint currentNumber = uint.Parse(Console.ReadLine());
            string currentNumberBinary = Convert.ToString(currentNumber, 2);
            
            int searchCount = 0;
            foreach (char currentBit in currentNumberBinary)
            {
                if (currentBit == bitToSearchfor)
                {
                    searchCount++;
                }
            }
            resultsArray[i] = searchCount;
        }

        foreach (int number in resultsArray)
        {
            Console.WriteLine(number);
        }
    }
}
